package com.gt.aep.base.config;

import com.gt.aep.base.service.systemManage.SysPermissionInitService;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ShrioConfig {
    @Autowired
    SysPermissionInitService spiService;
    //验证方式加入容器
    @Bean
    public ShrioRealm shiroRealm(){
        ShrioRealm shiroRealm=new ShrioRealm();
        return shiroRealm;
    }

    //权限管理
    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager sM=new DefaultWebSecurityManager();
        sM.setRealm(shiroRealm());
        return sM;
    }
    //设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        //登录
        shiroFilterFactoryBean.setLoginUrl("/login");
        //首页
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
        //自定义过滤器
        Map<String, Filter> filter=new HashMap<>();
        shiroFilterFactoryBean.setFilters(filter);
        //拦截器
        Map<String,String> filterMap =new HashMap<>();
        //权限添加
        System.out.println("权限菜单有"+spiService.getList());
        List<Map<String,Object>> maps=spiService.getList();
        for (Map<String, Object> map : maps) {
          //这块是角色访问
        }
        //登录页面访问
        filterMap.put("/login","anon");
        //登录资源访问
        filterMap.put("/user/login","anon");
        filterMap.put("/user/logout","anon");
        filterMap.put("/dtodrelation/..","anon");
        //角色权限访问
    /*    filterMap.put("/go/index","roles[管理员]");*/
      /*  filterMap.put("/go/index","authc");*/
        //匿名访问静态资源
        filterMap.put("/static/login","anon");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        System.out.println("Shiro拦截器");
        return shiroFilterFactoryBean;
    }

}
